//
//  OADTitleContentCell.m
//  OASDK_Works
//
//  Created by ChenJie on 3/2/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADTitleContentCell.h"
#import "OASDK.h"

#define kTitleFont [UIFont boldSystemFontOfSize:16.0f]

@interface OADTitleContentCell ()

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *separator;

@end

@implementation OADTitleContentCell

#pragma mark - Lifecycle methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = OAColor(f8f8f8);
        
        [self.contentView addSubview:self.iconView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.separator];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    self.iconView.frame = [self.viewModel.layouts[@"iconView"] CGRectValue];
    self.titleLabel.frame = [self.viewModel.layouts[@"titleLabel"] CGRectValue];
    self.separator.frame = [self.viewModel.layouts[@"separator"] CGRectValue];
    
    [self.contentView oa_showBounds];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    //
    [self clearCellContent];
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)clearCellContent {
    self.titleLabel.text = nil;
    self.iconView.image = nil;
}

#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - Protocol methods

#define kMarginLeft 15
#define kMarginRight 15
#define kMarginTop 10
#define kMarginBottom 10
#define kIconViewMarginLeft 9
#define kIconViewDimension 23
#define kTitleLabelMarginLeft 9
#define kSeparatorHeight 0.5

#pragma mark - <OADCellProtocol>

+ (void)layoutsWithViewModel:(OADTitleContentCellViewModel *)viewModel
            staticCellHeight:(CGFloat)staticCellHeight {
    
    CGFloat leftMark = 0;
    CGFloat rightMark = 0;
    CGFloat topMark = 0;
    
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    
    //
    leftMark = kMarginLeft;
    topMark = (staticCellHeight - kIconViewDimension) / 2;
    CGRect iconViewFrame = CGRectMake(leftMark, topMark, kIconViewDimension, kIconViewDimension);
    layouts[@"iconView"] = [NSValue valueWithCGRect:iconViewFrame];
    leftMark += kIconViewDimension;
    
    //
    leftMark += kTitleLabelMarginLeft;
    rightMark = kMarginRight;
    CGFloat titleLabelWidthMax = OA_SCREEN_WIDTH - leftMark - rightMark;
    
    CGSize titleLabelSize = [viewModel.title oa_sizeWithWidth:titleLabelWidthMax font:kTitleFont];
    
    topMark = (staticCellHeight - titleLabelSize.height) / 2;
    CGRect titleLabelFrame = CGRectMake(leftMark, topMark, titleLabelSize.width, titleLabelSize.height);
    layouts[@"titleLabel"] = [NSValue valueWithCGRect:titleLabelFrame];
    
    //
    CGFloat separatorTop = staticCellHeight - kSeparatorHeight;
    CGRect separatorFrame = CGRectMake(0, separatorTop, OA_SCREEN_WIDTH, kSeparatorHeight);
    layouts[@"separator"] = [NSValue valueWithCGRect:separatorFrame];
    
    //
    viewModel.layouts = layouts;
    
}

+ (CGFloat)cellHeightLayoutsWithViewModel:(OADTitleContentCellViewModel *)viewModel
                                   layout:(OADLayout *)layout{
    
    CGFloat cellHeight = 0;
    CGFloat leftMark = 0;
    CGFloat rightMark = 0;
    CGFloat topMark = 0;
    
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    
    //
    leftMark = kMarginLeft;
    CGRect iconViewFrame = CGRectMake(leftMark, topMark, kIconViewDimension, kIconViewDimension);
    layouts[@"iconView"] = [NSValue valueWithCGRect:iconViewFrame];
    leftMark += kIconViewDimension;
    cellHeight = MAX(cellHeight, iconViewFrame.size.height);
    
    //
    leftMark += kTitleLabelMarginLeft;
    rightMark = kMarginRight;
    
    topMark = kMarginTop;
    CGFloat titleLabelWidthMax = OA_SCREEN_WIDTH - leftMark - rightMark;
    CGSize titleLabelSize = [viewModel.title oa_sizeWithWidth:titleLabelWidthMax font:kTitleFont];
    CGRect titleLabelFrame = CGRectMake(leftMark, topMark, titleLabelSize.width, titleLabelSize.height);
    layouts[@"titleLabel"] = [NSValue valueWithCGRect:titleLabelFrame];
    topMark += titleLabelSize.height;
    cellHeight = MAX(cellHeight, topMark + kMarginBottom);
    
    //
    iconViewFrame.origin.y = (cellHeight - iconViewFrame.size.height) / 2;
    layouts[@"iconView"] = [NSValue valueWithCGRect:iconViewFrame];
    
    //
    CGFloat separatorTop = cellHeight - kSeparatorHeight;
    CGRect separatorFrame = CGRectMake(0, separatorTop, OA_SCREEN_WIDTH, kSeparatorHeight);
    layouts[@"separator"] = [NSValue valueWithCGRect:separatorFrame];
    
    viewModel.layouts = layouts;
    
    return cellHeight;
}

+ (OADLayout *)layoutWithViewModel:(id<OADCellViewModelProtocol>)viewModel
                  staticCellHeight:(CGFloat)staticCellHeight {
    return nil;
}

+ (OADLayout *)layoutWithViewModel:(id<OADCellViewModelProtocol>)viewModel {
    return nil;
}

#pragma mark - Setter

- (void)setViewModel:(OADTitleContentCellViewModel *)viewModel {
    _viewModel = viewModel;
    
    self.titleLabel.text = viewModel.title;
    self.iconView.image = [UIImage imageNamed:viewModel.iconName];
}

#pragma mark - Getter

- (UIImageView *)iconView {
    if (!_iconView) {
        UIImageView *imageView = [UIImageView new];
        
        _iconView = imageView;
    }
    return _iconView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        UILabel *label = [UILabel new];
        label.font = kTitleFont;
        label.textColor = OAColor(333333);
        label.numberOfLines = 0;
        
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UIView *)separator {
    if (!_separator) {
        UIView *view = [UIView new];
        view.backgroundColor = OAColor(e9e9e9);
        
        _separator = view;
    }
    return _separator;
}

#pragma mark - Memory Management Methods

- (void)dealloc {
}

@end
